/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.ddtroops.serviceclasses;

import cz.muni.fi.pa165.ddtroops.daointerfaces.SquadDAO;
import cz.muni.fi.pa165.ddtroops.dto.DTOFactory;
import cz.muni.fi.pa165.ddtroops.dto.HeroDTO;
import cz.muni.fi.pa165.ddtroops.dto.SquadDTO;
import cz.muni.fi.pa165.ddtroops.entities.EntityBuilder;
import cz.muni.fi.pa165.ddtroops.entities.Hero;
import cz.muni.fi.pa165.ddtroops.entities.Squad;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.SquadService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Erik
 */
@Service
@Qualifier("squadService")
public class SquadServiceImpl implements SquadService {

    @Autowired(required=true)
    private SquadDAO dao;

    public void setDao(SquadDAO dao) {
        this.dao = dao;
    }
    
    @Transactional
    @Secured("ROLE_SUPERVISOR")
    public SquadDTO createNewSquad(String name, String description, HeroDTO leaderDTO)
    {
        List<Hero> m = new ArrayList<Hero>();

        Hero leader = DTOFactory.createHero(leaderDTO);
        m.add(leader);

        Squad s = new EntityBuilder<Squad>(Squad.class)
                .withProperty("Name", name)
                .withProperty("Description", description)
                .withProperty("Task", "")
                .withProperty("Members", m)
                .withProperty("Leader", leader)
                .withProperty("Gold", 0L)
                .Build();

        return DTOFactory.createSquadDTO(s);
    }

    @Transactional
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public void create(SquadDTO squadDTO)
    {
        dao.create(DTOFactory.createSquad(squadDTO));
    }

    @Transactional
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public void update(SquadDTO squadDTO)
    {
        dao.update(DTOFactory.createSquad(squadDTO));
    }

    @Transactional
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public void delete(SquadDTO squadDTO)
    {
        dao.delete(DTOFactory.createSquad(squadDTO));
    }

    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public SquadDTO getByName(String name)
    {
        List<Squad> l = dao.getAll();
        for (Squad s : l) {
            if(s.getName().equals(name)) return DTOFactory.createSquadDTO(s);
        }
        return null;
    }

    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public SquadDTO getById(Long id)
    {
        Squad h = dao.getById(id);
        return (h!=null)?DTOFactory.createSquadDTO(h):null;
    }

    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public List<SquadDTO> getAll()
    {
        List<SquadDTO> squadDTOs = new ArrayList<SquadDTO>();
        for (Squad s : dao.getAll()) {
            squadDTOs.add(DTOFactory.createSquadDTO(s));
        }
        return squadDTOs;
    }
    @Transactional(readOnly=true)
    @Secured("IS_AUTHENTICATED_ANONYMOUSLY")
    public SquadDTO getSquadOfHero(HeroDTO hero) {
        Squad s = dao.getSquadOfHero(DTOFactory.createHero(hero));
        return (s==null)?null:DTOFactory.createSquadDTO(s);
    }
}
